<?php

namespace App\Http\Controllers\User;

use App\Http\Requests\DepartmentRequest;
use App\Http\Requests\DepartmentUpRequest;
use App\Models\Department;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class DepartmentController extends Controller
{
    public function index(){
        $department = Department::where('active','0')->paginate(9);
        return view('akl.Admin.Department_index',compact('department'));
    }

    public function create(){
        return view('akl.Admin.Department_add');
    }

    public function store( DepartmentRequest $request){
        $inp = Input::all();
        $inp['logo'] = $request->get('imgs')[0];
        $rel = Department::create($inp);
        if($rel->wasRecentlyCreated) return back()->with('errors',"添加成功");
        return back()->with('errors',"添加失败，请重试");
    }

    public function edit($id){
        $department = Department::where('active','0')->find($id);
        return view('akl.Admin.Department_edit',compact('department'));
    }

    public function update( DepartmentUpRequest $request,$id ){
        $inp = array(
            'name'=>Input::all()['name'],
            'introduce'=>Input::all()['introduce'],
            'domain'=>Input::all()['domain'],
            'water'=>Input::all()['water'],
            'botton'=>Input::all()['botton'],
        );
        if($request->get('imgs')[0]) $inp['logo'] = $request->get('imgs')[0];
        $rel = Department::where('id',$id)->update($inp);
        if($rel) return back()->with('errors',"更新成功");
        return back()->with('errors',"更新失败，请重试");
    }

    public function destroy($id){
        $rel = Department::where('id',$id)->update(['active'=>'1']);
        if($rel) return back()->with('errors',"删除成功");
        return back()->with('errors',"删除失败，请重试");
    }


}
